<template>
  <div>
    <div v-if="mode === 'read'" class="read-text">
      <span class="text">{{ formatDate(curBizObject[card.cardCode]) }}</span>
    </div>
    <a-date-picker
      v-else
      class="card-datetime"
      show-time
      v-model:value="val"
      :format="dataFormat.defaultValue"
      :valueFormat="dataFormat.defaultValue"
      :placeholder="placeholder?.label"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue';
import dayjs from 'dayjs';
import { DataFormatTimeOptions } from '@/model/objectMap/controlMap';

export default defineComponent({
  name: 'CardDatetime',
  props: {
    mode: {
      type: String,
      default: '',
    },
    card: {
      type: Object as any,
      default: () => ({}),
    },
    curBizObject: {
      type: Object as any,
      default: () => ({}),
    },
  },
  setup(props, context) {
    const ctlValue = computed(() => props.card.controls.ctlValue);
    const dataFormat = computed(() => props.card.controls.dataFormat);
    const formatDate = (date: string) => {
      if (!date) return null;
      return dayjs(date).format(dataFormat.value.defaultValue);
    };
    const val = computed({
      get: () => {
        const value: any = ctlValue.value.value ?? ctlValue.value.defaultValue;
        const date = formatDate(value);
        return date;
      },
      set: (val) => {
        context.emit('updateValue', val, 'value', props.card.controls.ctlValue);
      },
    });
    const placeholder = computed(() =>
      DataFormatTimeOptions.find(
        (it) => it.value === dataFormat.value.defaultValue,
      ),
    );

    return {
      val,
      dataFormat,
      placeholder,
      formatDate,
    };
  },
});
</script>

<style lang="less" scoped>
.card-datetime {
  width: 100%;
  font-size: 12px;
  line-height: 22px;
  border-radius: 4px;
}
.read-text {
  padding: 6px 12px;
  background-color: @rb-background-color;
  .text {
    display: inline-block;
    line-height: 22px;
  }
}
</style>
